<?php
//后台登录控制器
class LoginController extends Controller {
	//显示登录页面
	public function loginAction(){
		include CUR_VIEW_PATH . "login.html";
	}

	//验证登录操作
	public function signinAction(){
		//1.收集表单数据，用户名和密码
		$username = trim($_POST['username']);
		$password = trim($_POST['password']);

		//单引号转义
		$username = addslashes($username);
		$password = addslashes($password);
		
		//2.验证和处理
		if ($username == '') {
			$this->jump('index.php?p=admin&c=login&a=login','用户名不能为空');
		}
		if ($password == '') {
			$this->jump('index.php?p=admin&c=login&a=login','密码不能为空');
		}
		//3.调用模型完成验证操作，给出相应提示，并跳转
		$adminModel = new AdminModel('admin');
		$user = $adminModel->checkUser($username,$password);
		if (!empty($user)) {
			//ok,登录成功，保存登录标识，跳转
			$_SESSION['admin'] = $user;
			$this->jump('index.php?p=admin&c=index&a=index','',0);
		} else {
			//failer
			$this->jump('index.php?p=admin&c=login&a=login','用户名或密码错误');
		}
	}

	//退出
	public function logoutAction(){
		//销毁session
		unset($_SESSION['admin']); //可加可不加 但不能这么写，unset($_SESSION);
		session_destroy();
		//跳转
		$this->jump('index.php?p=admin&c=login&a=login','',0);
	}

	//生成验证码
	public function captchaAction(){
		//载入验证码类
		$this->library('Captcha');
		//实例化对象
		$captcha = new Captcha();
		//生成验证码
		$captcha->generateCode();
		//将其保存到session
		$_SESSION['captcha'] = $captcha->getCode();
	}
}

